
#include "StdAfx.h"
#include "ColorCombo.h"
#include "CommonDef.h"
#include "CommonFun.h"

CColorComboBox::CColorComboBox()
{
	for(int i = 0; i < MAX_SUPPORT_WA_COUNT; i++)
	{
		COLORREF crColor = GetWAColor(i);
		m_vecColors.push_back(crColor);
	}
}

CColorComboBox::~CColorComboBox()
{
}


BEGIN_MESSAGE_MAP(CColorComboBox, CComboBox)
//{{AFX_MSG_MAP(CColorComboEx)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorComboEx message handlers

void CColorComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);
	CRect rect(&(lpDrawItemStruct->rcItem));
		
	// Create the brush
	CBrush brush(m_vecColors[lpDrawItemStruct->itemID]);
	CRect rectItem(&(lpDrawItemStruct->rcItem));

	rectItem.InflateRect(-2, -2);

	// Color the area
	dc.FillRect(rectItem, &brush);

	// Draw the focus rect if the mouse is either over the item, or if the item
	// is selected
	if (lpDrawItemStruct->itemState & ODS_SELECTED)
		dc.DrawFocusRect(rectItem);
	
	// This draws the black frame around each of the colors so that they
	// do not look like they are kind of blended together
	CBrush frameBrush(RGB(0, 0, 0));
	dc.FrameRect(rect, &frameBrush);
	rect.InflateRect(-1, -1);
	
	dc.Detach();
}

void CColorComboBox::PreSubclassWindow() 
{
	for (int nColors = 0; nColors < m_vecColors.size(); nColors++)
		// Add a dummy string for every array item so that WM_DRAWITEM message is sent.
		AddString(_T(""));
	
	// Select the first color when the control is created.
	SetCurSel(0);
	
	CComboBox::PreSubclassWindow();
}

COLORREF CColorComboBox::GetWAColor(int nIndex)
{
	CString strIniFile = GetExePath();
	strIniFile += _T("\\");
	strIniFile += GYWA_INI_FILE;

	CString strColorKey;
	strColorKey.Format(_T("Color%d_RGB"), nIndex + 1);
	
	TCHAR szValue[MAX_PATH] = {0};
	::GetPrivateProfileString(_T("Color"), strColorKey, _T(""), szValue, MAX_PATH, strIniFile);

	CString strRGB = szValue;
	int nPos1 = strRGB.Find(_T(","));
	int nPos2 = strRGB.Find(_T(","), nPos1 + 1);
	if(nPos1 > 0 && nPos2 > 0 && nPos2 > nPos1)
	{
		CString strR = strRGB.Left(nPos1);
		CString strG = strRGB.Mid(nPos1 + 1, nPos2 - nPos1 - 1);
		CString strB = strRGB.Mid(nPos2 + 1);

		COLORREF crRet = RGB(_ttoi(strR), _ttoi(strG), _ttoi(strB));
		return crRet;
	}	

	ASSERT(FALSE);
	return RGB(0, 0, 0);
}